package APP1.bookShop.main;

import APP1.GUI2.MainFrame_2;
import APP1.bookShop.people.Author;
import APP1.bookShop.people.Publisher;
import APP1.bookShop.book.Book;
import App1.bookShop.controllers.AuthorController;
import App1.bookShop.controllers.BookController;
import APP1.bookShop.order.Order;
import APP1.bookShop.order.OrderBody;
import APP1.bookShop.stock.Stock;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;

import App1.bookShop.controllers.OrderController;
import App1.bookShop.controllers.PublisherController;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import java.util.Map;
import java.util.Random;
import java.util.Set;

/**
 *
 * @author Asus
 */
public class BookShop {

    public static String printDate(Date d) {
        return d.getDay() + "\\" + d.getMonth() + "\\" + d.getYear();
    }
    //constants
    public static final String EBOOK = "EBook";
    public static final String PAPERBOOK = "Paper";

    //gui
    private MainFrame_2 GUI;

    private List<Book> books;
    private List<Author> authors;
    private List<Publisher> publishers;
    private List<Order> orders;
    private Map<Book, Stock> bookStocks;

    //Controllers
    private AuthorController authorController;
    private BookController bookController;
    private PublisherController publisherController;
    private OrderController ordersController;

    public BookShop() {
        initialize();
        createControllers();
        startGUI();

        //debug methods
        debugAddAuthor(12);
       
        debugAddPublisher(7);
 debugAddBooks(10);
        debugOrder(10);

    }

    private void debugAddAuthor(int num) {
        for (int i = 0; i < num; i++) {
            Author a = new Author();
            a.setFirstName("authorName" + i);
            a.setLastName("authorSurname" + i);
            authors.add(a);
        }
    }

    private void debugAddBooks(int num) {
        for (int i = 0; i < num; i++) {
            Book b = new Book();
            b.setTitle("book" + i);
            b.setIsbn(Math.random() + 1000000 + i / 100000 + "");

            DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
            //get current date time with Date()
            Date date = new Date();
            b.setPublishingDate(date);
            int rand = (new Random().nextInt(authors.size()));
            b.addAuthor(authors.get(rand));
             rand = (new Random().nextInt(publishers.size()));
            b.addPublisher(publishers.get(rand));
            
            
            if(!getBookStocks().containsKey(b)){
                getBookStocks().put(b, new Stock(0));
            }
            
            books.add(b);
        }
    }

    private void debugAddPublisher(int num) {
        for (int i = 0; i < num; i++) {
            Publisher p = new Publisher("pub" + i, "pubMail" + i, "phone" + i, "postalCode" + i);
           
           
            publishers.add(p);
        }
    }

    private void debugOrder(int num) {
        DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
        //get current date time with Date()
        Date date = new Date();
        for (int i = 0; i < num; i++) {
            Order o = new Order(date, date, 10 * i, publishers.get((new Random().nextInt(publishers.size()))));
            o.setProcessed(false);
            
          
            orders.add(o);
        }
    }

    private void initialize() {
        this.books = new ArrayList();
        this.authors = new ArrayList();
        this.publishers = new ArrayList();
        this.orders = new ArrayList();
        this.bookStocks = new HashMap();

    }

    public BookController getBokController() {
        return bookController;
    }

    private void startGUI() {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(MainFrame_2.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(MainFrame_2.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(MainFrame_2.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(MainFrame_2.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                GUI = new MainFrame_2(BookShop.this);
                GUI.pack();
                GUI.setVisible(true);

            }
        });

    }

    public static void main(String[] args) {
        BookShop bookshop;
        bookshop = new BookShop();
    }

    private void createControllers() {
        authorController = new AuthorController(this);
        bookController = new BookController(this);
        publisherController = new PublisherController(this);
        ordersController = new OrderController(this);

    }

    public BookController getBookController() {
        return bookController;
    }

    public OrderController getOrdersController() {
        return ordersController;
    }

    public AuthorController getAuthorController() {
        return authorController;
    }

    public List<Book> getBooks() {
        return books;
    }

    public List<Author> getAuthors() {
        return authors;
    }

    public List<Publisher> getPublishers() {
        return publishers;
    }

    public List<Order> getOrders() {
        return orders;
    }

    public Map<Book, Stock> getBookStocks() {
        return bookStocks;
    }

    public MainFrame_2 getGUI() {
        return GUI;
    }

    public PublisherController getPublisherController() {
        return publisherController;
    }

}
